package com.mlink.ai.chat.ui.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.ItemExploreCreateBinding
import com.mlink.ai.chat.assistant.robot.databinding.ItemExploreSubCategoryBinding
import com.mlink.ai.chat.bean.ExploreCategory
import com.mlink.ai.chat.bean.ExploreSubCategory
import com.mlink.ai.chat.utils.PromptUtils
import com.mlink.ai.chat.utils.ScreenUtils
import com.mlink.ai.chat.utils.dp2px

class ExploreSubCategoryAdapter :
    ListAdapter<ExploreSubCategory, RecyclerView.ViewHolder>(MyDiffCallback()) {
    private var listener: OnItemOperateListener? = null

    companion object {
        private const val NORMAL_ITEM_VIEW_TYPE = 0
        private const val CREATE_ITEM_VIEW_TYPE = 1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (CREATE_ITEM_VIEW_TYPE == viewType) {
            return CreateViewHolder(
                ItemExploreCreateBinding.inflate(
                    LayoutInflater.from(parent.context), parent, false
                )
            )
        }
        return NormalViewHolder(
            ItemExploreSubCategoryBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder.itemView.layoutParams.width <= 0) {
            val itemWidth =
                (ScreenUtils.getScreenWidth(holder.itemView.context) - 2 * holder.itemView.context.resources.getDimensionPixelSize(
                    R.dimen.explore_list_padding_horizontal
                ) - 13.dp2px()) / 2
            holder.itemView.layoutParams.width = itemWidth
        }
        val item = getItem(position)
        if (holder is NormalViewHolder) {
            holder.bind(item)
        } else if (holder is CreateViewHolder) {
            holder.bind()
        }
    }

    override fun getItemViewType(position: Int): Int {
        val data = getItem(position)
        if (data.id == PromptUtils.PROMPT_CREATE_ITEM_ID) {
            return CREATE_ITEM_VIEW_TYPE
        }
        return NORMAL_ITEM_VIEW_TYPE
    }

    inner class NormalViewHolder(val binding: ItemExploreSubCategoryBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(category: ExploreSubCategory) {
            binding.title.text = category.categoryName
            binding.desc.text = category.desc
            binding.tvEmoji.apply {
                visibility = if (category.isCustom) View.INVISIBLE else View.VISIBLE
                text = category.emoji
            }
            binding.ivDelete.run {
                visibility = if (category.isCustom) View.VISIBLE else View.INVISIBLE
                setOnClickListener {
                    listener?.onDeleteShow(binding.deleteItem.root)
                    showDeleteItem()
                }
                binding.deleteItem.noIv.setOnClickListener {
                    hideDeleteItem()
                }
                binding.deleteItem.yesIv.setOnClickListener {
                    hideDeleteItem()
                    val categoryList = PromptUtils.getCustomPromptCategoryList() as ArrayList
                    var deleteCategory: ExploreCategory? = null
                    categoryList.forEach {
                        if (it.id == category.parentCategoryId) {
                            (it.subCategories as ArrayList).remove(category)
                        }
                        if (it.subCategories.isEmpty() && it.isCustom) {
                            deleteCategory = it
                        }
                    }
                    categoryList.remove(deleteCategory)
                    PromptUtils.saveCustomPromptCategoryList(categoryList)
                    listener?.deleteItem(absoluteAdapterPosition, category)
                }
            }
            binding.root.setOnClickListener {
                listener?.onItemClicked(category)
            }
        }

        private fun hideDeleteItem() {
            binding.deleteItem.root.visibility = View.GONE
        }

        private fun showDeleteItem() {
            binding.deleteItem.root.visibility = View.VISIBLE
        }
    }

    inner class CreateViewHolder(val binding: ItemExploreCreateBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind() {
            binding.root.setOnClickListener {
                listener?.onCreateItemClicked()
            }
        }
    }

    class MyDiffCallback : DiffUtil.ItemCallback<ExploreSubCategory>() {
        override fun areItemsTheSame(
            oldItem: ExploreSubCategory, newItem: ExploreSubCategory
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: ExploreSubCategory, newItem: ExploreSubCategory
        ): Boolean {
            return oldItem == newItem
        }
    }

    fun setOnItemOperateListener(listener: OnItemOperateListener) {
        this.listener = listener
    }

    interface OnItemOperateListener {

        fun onCreateItemClicked()

        fun onItemClicked(subCategory: ExploreSubCategory)

        fun deleteItem(position: Int, subCategory: ExploreSubCategory)

        fun onDeleteShow(view : View)
    }
}